package tracks.client.map;

import java.util.ArrayList;
import java.util.Collection;

public class MapEventNotifier {

	private final Collection<IMapEventListener> selectionListeners = new ArrayList<IMapEventListener>();

	/**
	 * Add a map listener. Each listener can only be registered once. This is
	 * handled for you in case you attempt to register the listener multiple
	 * times.
	 * 
	 * @param listener
	 *            <tt>IMapSelectionListener</tt> to add.
	 */
	public void addListener(IMapEventListener listener) {
		if (listener != null) {
			if (!selectionListeners.contains(listener)) {
				selectionListeners.add(listener);
			}
		}
	}

	/**
	 * Removes the specified listener.
	 * 
	 * @param listener
	 *            <tt>IMapSelectionListener</tt> to remove.
	 */
	public void removeListener(IMapEventListener listener) {
		if (listener != null) {
			selectionListeners.remove(listener);
		}
	}

	/**
	 * Notifies all listeners of the <tt>MapEvent</tt>.
	 * 
	 * @param mapEvent
	 *            Event that is passed along.
	 */
	public void notifyListeners(MapEvent mapEvent) {
		// No listeners will ever be null, so let's be merry and notify.
		for (IMapEventListener listener : selectionListeners) {
			listener.notify(mapEvent);
		}
	}

}
